import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 * Description:
 * User: Administrator
 * Data: 2023 - 09 - 04
 * Time: 20:55
 */
//牛客 数组中出现次数超过一半的数字
public class Solution2 {
    //官方推荐方法
    public static int MoreThanHalfNum_Solution (int[] numbers) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            if(map.containsKey(numbers[i])) {
                map.put(numbers[i], map.get(numbers[i]) + 1);
            }else {
                map.put(numbers[i], 1);
            }
            if(map.get(numbers[i]) > numbers.length / 2) {
                return numbers[i];
            }
        }
        return -1;
    }
}
